package controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import serivce.OrderService;
import serivce.ReturnMessage;
import serivce.UserSerivce;
import untity.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/personal")
public class Personal {

    @Autowired
    private UserSerivce userSerivce;

    @Autowired
    @Qualifier("chatList")
    private List<ChatRoom> chatList;

    @Autowired
    private OrderService orderService;

    @PostMapping("/collection")
    public List<BriefHouse> collection(@RequestBody JSONObject data,HttpSession session){
        Integer count=data.getInteger("count");
        System.out.println(session);
        String id=(String)session.getAttribute("id");
        System.out.println(id);
        List<BriefHouse> list= userSerivce.searchByUserID(id,count);
        for (BriefHouse b: list) {
            System.out.println(b);
        }
        return list;
    }

    @PostMapping("/myRental")
    public List<Myrental> myRental(@RequestBody JSONObject data,HttpSession session){
        Integer count=data.getInteger("count");
        String id=(String)session.getAttribute("id");
        List<Myrental> list=userSerivce.searchMyRental(id,count);
        return list;
    }

    @PostMapping("/history")
    public List<Histroy> history(@RequestBody JSONObject data, HttpSession session){
        Integer count=data.getInteger("count");
        String id=(String)session.getAttribute("id");
        List<Histroy> list=userSerivce.searchHistry(id,count);
        return list;
    }

    @PostMapping("/connection")
    public List<UserConnection> connectionList(HttpSession session){
        String id=(String) session.getAttribute("id");
        List<UserConnection> list= userSerivce.searchConnetion(id);

        return list;
    }

    @PostMapping("/check")
    public User checkSession(HttpSession session){
        User user=new User();
        String id=(String)session.getAttribute("id");
        if (id==null){
            return null;
        }
        String userName=(String)session.getAttribute("userName");
        user.setUserName(userName);
        user.setId(Integer.valueOf(id));
        return user;
     }

     @PostMapping("/chat/{id}")
    public JSONObject sendMessage(@PathVariable("id") String roomid, @RequestBody Chatmessage chatmessage, HttpSession session){
         System.out.println(chatmessage);
         String id=(String)session.getAttribute("id");
         chatmessage.setUserId(Integer.valueOf(id));
         if (chatList.isEmpty()){
             ChatRoom c= new ChatRoom();
             c.setId(roomid);
             chatList.add(c);
         }
         for (ChatRoom c:chatList) {
             if (c.getId().equals(roomid)){
                 c.putmessage(chatmessage);
                 System.out.println(chatmessage);
                 break;
             }
         }
        return null;
     }

     @ResponseBody
     @PostMapping("/getMessage")
     public List<ReturnMessage> getMessage(HttpSession session){
         List<ReturnMessage> messagesList=new ArrayList<>();
        String id=(String) session.getAttribute("id");
        List<String> list=userSerivce.searchRoomId(id);
        if (chatList.isEmpty()){
            for (String roomid:list){
                ChatRoom c= new ChatRoom();
                c.setId(roomid);
                chatList.add(c);
            }
        }
        for(ChatRoom c:chatList){
            for (String roomid:list){
                if (c.getId().equals(roomid)){
                    System.out.println("c===="+c);
                    List list1=c.getmessage(Integer.valueOf(id));
                    if (list1==null){
                        return null;
                    }else {
                        ReturnMessage returnMessage=new ReturnMessage();
                        returnMessage.setRoomId(roomid);
                        returnMessage.setList(list1);
                        System.out.println(returnMessage);
                        messagesList.add(returnMessage);
                    }

                }
            }
        }
        if (messagesList.isEmpty()){
            return null;
        }else {
//            System.out.println(messagesList.get(0));
            for(ReturnMessage me:messagesList){
                System.out.println(me);
            }
            return messagesList;
        }

     }


     @PostMapping("/historyMessage")
    public List<ReturnMessage> historyMessage(@RequestBody JSONObject roomlist,HttpSession session){
        List<ReturnMessage> list=new ArrayList<>();
        List<String> a= roomlist.getJSONArray("roomlist").toJavaList(String.class);
         for (String id:a) {
             System.out.println("aaaaaa==="+id);
         }
        for (ChatRoom c: chatList) {
            for (String roomId:a) {
                System.out.println("roomid"+roomId);
                if (c.getId().equals(roomId)){
                    ReturnMessage returnMessage=new ReturnMessage();
                    List<Chatmessage> list1=new ArrayList<>();
                    list1.addAll(c.getOldMessage());
                    list1.addAll(c.getNewMessage());
                    returnMessage.setRoomId(roomId);
                    returnMessage.setList(list1);
                    list.add(returnMessage);
                }
            }
         }
        if (!list.isEmpty()){
            return list;
        }else {
            return null;
        }
     }

     @PostMapping("/myOrder")
    public List<Order> myOrder(HttpSession session){
        String id=(String) session.getAttribute("id");
         List<Order> list= orderService.searchOrder(id);
        return list;
     }

     @PostMapping("/change")
    public Boolean changeData(@RequestBody BriefHouse house){
        Boolean flag=false;
        flag=userSerivce.changeData(house);
        return flag;
     }

     @PostMapping("/delete/{id}")
    public void delete(String id){
        userSerivce.delete(id);
     }
}
